package com.aispeech.redis.test;

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class StreamProducer {
    public static void main(String[] args) {
        int nbOfMessageToSend = 1;

        RedisClient redisClient = RedisClient.create(RedisConf.CONN_STRING); // change to reflect your environment
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        RedisCommands<String, String> syncCommands = connection.sync();

        for (int i = 0; i < nbOfMessageToSend; ++i) {
            Map<String, String> messageBody = new HashMap<>();
            messageBody.put("ts", String.valueOf(System.currentTimeMillis()));
            messageBody.put("index", String.valueOf(i));

            String messageId = syncCommands.xadd(
                    RedisConf.KEY,
                    messageBody);

            log.info("Message {} : {} posted", messageId, messageBody);
        }

        connection.close();
        redisClient.shutdown();
    }
}
